package com.xxxy.controller;

import com.xxxy.commonResult.CommonResult;
import com.xxxy.log.Log;
import com.xxxy.model.Admin;
import com.xxxy.model.Student;
import com.xxxy.model.Teacher;
import com.xxxy.redis.service.RedisService;
import com.xxxy.service.AdminService;
import com.xxxy.service.StudentService;
import com.xxxy.service.TeacherService;
import com.xxxy.util.JwtUtils;
import com.xxxy.util.Md5EncryptUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RestController
@Api(tags = "用户登录")
public class AuthController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    RedisService redisService;

    @Log(module = "用户登录", description = "用户登录")
    @GetMapping(value = "/auth/login")
    @ApiOperation(value = "用户登录(1=学生，2=教师，3=管理员)")
    public CommonResult<Map<String, Object>> login(@ApiParam("账号") @RequestParam String account,
                                                   @ApiParam("密码") @RequestParam String pwd) {
        Object user = null;
        int i = 0;
        pwd = Md5EncryptUtil.md5Encrypt(pwd);
        if (account.length() == 10) {
            user = studentService.loginByStudent(account, pwd);
            i = 1;
        } else if (account.length() == 6) {
            user = teacherService.loginByTeacher(account, pwd);
            i = 2;
        } else {
            user = adminService.loginByAdmin(account, pwd);
            i = 3;
        }
        if (user != null) {
            String token = "";
            //redis存储
            Map<String, Object> params = new HashMap<>();
            //返回的数据
            Map<String, Object> paramsResponse = new HashMap<>();
            Set<String> roles = new HashSet<>();
            if (i == 1) {
                Student student = (Student) user;
                token = jwtUtils.generateToken(student.getAccount(), 1);
                roles.add("STUDENT");
                params.put("roles", roles);
                redisService.hSetAll(student.getAccount(), params, jwtUtils.getExpiration());
                paramsResponse.put("role", 1);
            } else if (i == 2) {
                Teacher teacher = (Teacher) user;
                token = jwtUtils.generateToken(teacher.getAccount(), 2);
                roles.add("TEACHER");
                params.put("roles", roles);
                redisService.hSetAll(teacher.getAccount(), params, jwtUtils.getExpiration());
                paramsResponse.put("role", 2);
            } else {
                Admin admin = (Admin) user;
                token = jwtUtils.generateToken(admin.getAccount(), 3);
                roles.add("ADMIN");
                params.put("roles", roles);
                redisService.hSetAll(admin.getAccount(), params, jwtUtils.getExpiration());
                paramsResponse.put("role", 3);
            }
            paramsResponse.put("token", token);
            return CommonResult.success(paramsResponse);
        }
        return null;
    }

    @Log(module = "用户登录", description = "用户登出")
    @GetMapping(value = "/auth/logout")
    @ApiOperation(value = "用户登出")
    public CommonResult<Map<String, Object>> logout(@ApiParam("账号") @RequestParam String account) {
        if (redisService.del(account)) {
            return CommonResult.success("登出成功");
        }
        return CommonResult.failed("登出失败");
    }
}
